<?php
namespace Home\Controller;
use Think\Controller;
class QuestionController extends CommonController 
{
	//发表问题页面
    public function index()
    {

    	//登陆信息显示
        $this->_userinfo($_SESSION['id']);
        //人类问题
		$question = D('vercode')->vercode();
		$this->assign('question',$question);

        //cate
        $cate = M('category')->select();
        $cate = $this->catetree($cate);
        $this->assign('cate',$cate);
        //kisslev
        $kisslev = M('kisslev')->select();
        $this->assign('kisslev',$kisslev);

        $this->assign('title','发表问题');
        $this->display(); 
    }
    //问题显示页面 ,解答显示页面
    public function question()
    {
        //print_r($_SESSION);
        $id = I('get.id');
        //问题表跟user表联查
        $sql = 'select t1.*,t2.username,t2.face from question t1 inner join user t2 on t1.uid=t2.id where t1.id='.$id;
        //echo $sql;
        $question = M()->query($sql);
        $question = current($question);
        $this->assign('question_msg',$question);
        //加入时间对比显示
        $question_time = run($question['create_time']);
        $this->assign('question_time',$question_time);

        //自动把view_num字段加一
        $this->auto_num(question,view_num,$id,1);

        //解答显示
        $sql = 'select t1.*,t2.username,t2.face from answer t1 inner join user t2 on t1.uid=t2.id where t1.qid='.$id;
        $answer = M()->query($sql);
        $this->assign('answer',$answer);

        //最近热帖 10篇
        $sql = 'select * from question order by view_num desc limit 10';
        $zuijinhot = M()->query($sql);
        $this->assign('zuijinhot',$zuijinhot);

        //最近热议 10篇
        $sql = 'select * from question order by answer_num desc limit 10';
        $zuijinhotyi = M()->query($sql);
        $this->assign('zuijinhotyi',$zuijinhotyi);

        $this->assign('title',$question['title']);
        $this->display();
    }

    //问题编辑显示
    public function question_edit()
    {
        $id = I('get.id');

        //问题对应显示
        $sql = 'select * from question where id='.$id;
        $question_edit = M()->query($sql);
        $this->assign('question_edit',current($question_edit));

        //人类问题
        $question = D('vercode')->vercode();
        $this->assign('question',$question);
        //cate
        $cate = M('category')->select();
        $cate = $this->catetree($cate);
        $this->assign('cate',$cate);
        //kisslev
        $kisslev = M('kisslev')->select();
        $this->assign('kisslev',$kisslev);

        $this->assign('title','问题编辑');
        $this->display();  

    }

    //问题编辑
    public function question_editsave()
    {
        //print_r($_POST);

        //更新
        if (IS_POST) {
            $data['id'] = I('post.id');
            $data['update_time'] = time();
            $data['content'] = I('post.content');
            $data['cid'] = I('post.class');
            $data['kiss'] = I('post.experience');

            //内容
            if (empty($data['content'])) {
                $arr = array(
                        "error" => 0,
                        "info" => "内容不能为空哦"
                    );
                echo json_encode($arr);
                exit;
            }
            //人类验证
            if ($_SESSION['vercode_answer']) {
                if ($_SESSION['vercode_answer']!=I('post.vercode')) {
                    $arr = array(
                        "error" => 0,
                        "info" => "人类问题错误"
                        );
                    echo json_encode($arr);
                    exit;
                }
            }
         
            //入库
            $res = M('question')->where(array('id'=>$data['id']))->save($data); 

            if ($res) {
                $url = U('home/question/question_edit',array('id'=>$data['id']));
                $arr = array(
                'error' => 1,
                'info' => '修改成功',
                'url' => $url
                );

                echo json_encode($arr);
            }else {
                $arr = array(
                'error' => 0,
                'info' => '修改失败'
                );

                echo json_encode($arr);
            }
        }
    }

    //发表问题
    public function question_add()
    {
    	//print_r($_POST);exit;
        // [title] => aaaaaaaaaa
        // [content] => aaaaaaaaaaaaaaaaaaaaaaaaaaa
        // [file] => 
        // [class] => 2
        // [experience] => 5
        // [vercode] => 4
        if (IS_POST) {
            $data['title'] = I('post.title');
            $data['content'] = I('post.content');
            $data['cid'] = I('post.class');
            $data['kiss'] = I('post.experience');
            $data['create_time'] = time();
            $data['uid'] = $_SESSION['id'];
            //标题
            if (strlen($data['title']) < 4 && strlen($data['title']) > 100) {
                $arr = array(
                        "error" => 0,
                        "info" => "标题必须是4~100位字符串"
                    );
                echo json_encode($arr);
                exit;
            }
            //内容
            if (empty($data['content'])) {
                $arr = array(
                        "error" => 0,
                        "info" => "内容不能为空哦"
                    );
                echo json_encode($arr);
                exit;
            }
            //人类验证
            if ($_SESSION['vercode_answer']) {
                if ($_SESSION['vercode_answer']!=I('post.vercode')) {
                    $arr = array(
                        "error" => 0,
                        "info" => "人类问题错误"
                        );
                    echo json_encode($arr);
                    exit;
                }
            }
        }
        //入库
        $res = M('question')->add($data);
        if ($res) {
            $url = U('home/question/question',array('id'=>$res));
            $arr = array(
            'error' => 1,
            'info' => '发布成功',
            'url' => $url
            );

            echo json_encode($arr);
        }else {
            $arr = array(
            'error' => 0,
            'info' => '发布失败'
            );

            echo json_encode($arr);
        }

    }


    //编辑器图片上传
    public function img()
    {
        //print_r($_POST);
        $upload = new \Think\Upload();// 实例化上传类    
        $upload->maxSize   =     3145728 ;// 设置附件上传大小    
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型    
        $upload->savePath  =      'Uploads/edit/'; // 设置附件上传目录    
        // 上传文件     
        $info   =   $upload->upload();    
        if(!$info) {// 上传错误提示错误信息         
            $arr = array(
            'code' => 0,
            'msg' => $upload->getError(),
            );
            echo json_encode($arr);
 
        }else{// 上传成功   
            $info = current($info);//减维 
            $url =  __ROOT__.'/'.'Uploads/'.$info['savepath'].$info['savename'];    

            $arr = array(
            'code' => 0,
            'msg' => '上传成功',
            'data'=> array(
                'src' => $url,
                'title' => $info['savename']
                )
            );
            echo json_encode($arr);
        }
    }

    //问题解答提交 入库
    public function answer_add()
    {
        //print_r($_POST);exit;
        if (IS_POST) {
            $data['qid'] = I('post.qid');
            $data['uid'] = I('post.uid');
            $data['content'] = I('post.content');
            $data['create_time'] = time();

            //无登陆不能回答 跳回登陆页面
            if (!isset($_SESSION['username'])) {
                $url = U('home/login/index');
                $arr = array(
                        "error" => 1,
                        "info" => "需要登录才能回答哦",
                        'url' => $url
                    );
                echo json_encode($arr);
                exit;
            }

            //内容
            if (empty($data['content'])) {
                $arr = array(
                        "error" => 0,
                        "info" => "内容不能为空哦"
                    );
                echo json_encode($arr);
                exit;
            }
            //入库
            $res = M('answer')->add($data);  
            //提交成功 question表answer_num 加一
            $this->auto_num(question,answer_num,$data['qid'],1);

            if ($res) {
                $url = U('home/question/question',array('id'=>$data['qid']));
                $arr = array(
                'error' => 1,
                'info' => '回答成功',
                'url' => $url
                );

                echo json_encode($arr);
            }else {
                $arr = array(
                'error' => 0,
                'info' => '回答失败'
                );

                echo json_encode($arr);
            }
        }

    }
    //点击回复把当前回答uid 异步过来
    public function answeruserid()
    {
        //print_r($_POST);
        $uid = I('post.answeruserid');
        //无登陆不能回答 跳回登陆页面
        if (IS_POST) {
            if (!isset($_SESSION['username'])) {
                $url = U('home/login/index');
                $arr = array(
                        "error" => 0,
                        "info" => "需要登录才能回答哦",
                        'url' => $url
                    );
                echo json_encode($arr);
                exit;
            }
        }
            
        //已登陆
        $sql = 'select t1.uid,t2.username from answer t1 inner join user t2 on t1.uid=t2.id where t1.uid='.$uid.' limit 1';
        $res = M()->query($sql);
        $res = current($res);
        $arr = array(
                "error" => 1,
                "info" => "你正在@ ".$res['username']." 的回答"
            );
        echo json_encode($arr);

    }

    //点赞功能 answer表 zan字段加一
    public function dianzan()
    {
        //print_r($_POST);exit;
        if (IS_POST) {
            $zan = I('post.zan');
            $row = $this->auto_num(answer,zan,$zan,1);
            if ($row) {
                $zan = M('answer')->where(array('id'=>$zan))->find();
                $arr = array(
                'error' => 1,
                'info' => '点赞成功',
                'zan' => $zan['zan']
                );
                echo json_encode($arr);
            }else {
                $arr = array(
                'error' => 0,
                'info' => '点赞失败'
                );
                echo json_encode($arr);
            }
        }
    }

    //采纳功能   异步过来id  更新answer question表
    public function caina_status()
    {
        //print_r($_POST);
        $id = I('post.statusid');
        if (IS_POST) {
            //UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值
            $caina_time = time();
            $sql = 'update answer set status=1, caina_time='. $caina_time .' where id='.$id;
            $answer_status = M()->execute($sql);
            if ($answer_status) {
                $sql = 'select * from answer where id='.$id;
                //取出question id
                $answer_qid = M()->query($sql);
                $answer_qid = current($answer_qid); 

                //同时更新question status 采纳
                $sql = 'update question set status="已采纳" where id='.$answer_qid['qid'];
                $question_status = M()->execute($sql);

                if ($question_status) {
                    $url = U('home/question/question',array('id'=>$answer_qid['qid']));
                    $arr = array(
                    'error' => 1,
                    'info' => '采纳成功',
                    'url' => $url
                    );
                    echo json_encode($arr);
                }else {
                    //question 表状态更新失败  answer状态回滚
                    $sql = 'update answer set status=0,caina_time=0 where id='.$id;
                    M()->execute($sql);

                    $arr = array(
                    'error' => 0,
                    'info' => '采纳失败'
                    );
                    echo json_encode($arr);
                }

            }else {
                $arr = array(
                'error' => 0,
                'info' => '采纳失败'
                );
                echo json_encode($arr);
            }
        }
    }








}